package com.yiwei.zxp.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.ShareGoldExplainBean;
import com.yiwei.zxp.api.model.MyShareGoldModel;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.constant.Const;
import com.yiwei.zxp.fragment.MyShareMoneyFragment;
import com.yiwei.zxp.fragment.MyShareMoneyTimeOutFragment;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.Spans;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.view.HomeViewPager;
import com.yiwei.zxp.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyShareMoneyActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.pager)
    HomeViewPager pager;
    @BindView(R.id.btn_work_ing)
    TextView btnWorkIng;
    @BindView(R.id.btn_work_over)
    TextView btnWorkOver;
    @BindView(R.id.btn_question)
    ImageView btnQuestion;
    @BindView(R.id.txt_right)
    ImageView txtRight;
    @BindView(R.id.txt_info_left)
    TextView txtInfoLeft;
    @BindView(R.id.txt_info_right)
    TextView txtInfoRight;
    @BindView(R.id.txt_balance)
    TextView txtBalance;

    private Unbinder unbiner;
    private int select = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_share_money);
        initBarTouming();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("我的淘淘币");
        Fragment[] fragments = new Fragment[2];
        for (int i = 0; i < 2; i++) {
            switch (i) {
                case 0:
                    MyShareMoneyFragment fragment = new MyShareMoneyFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", i);
                    fragment.setArguments(bundle);
                    fragments[i] = fragment;
                    break;
                case 1:
                    MyShareMoneyTimeOutFragment fragment1 = new MyShareMoneyTimeOutFragment();
                    Bundle bundle1 = new Bundle();
                    bundle1.putInt("id", i);
                    fragment1.setArguments(bundle1);
                    fragments[i] = fragment1;
                    break;
            }
        }

        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }

        });
        getMyShareGold();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    private void showTextChange(boolean isShow) {
        if (isShow) {
            btnWorkIng.setTextColor(0xffffffff);
            btnWorkIng.setBackgroundResource(R.drawable.btn_red_radius_20);
            btnWorkOver.setTextColor(0xff999999);
            btnWorkOver.setBackgroundResource(0);
        } else {
            btnWorkIng.setTextColor(0xff999999);
            btnWorkIng.setBackgroundResource(0);
            btnWorkOver.setTextColor(0xffffffff);
            btnWorkOver.setBackgroundResource(R.drawable.btn_red_radius_20);
        }
    }

    public void getMyShareGold() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

//        loadingDialog.show();

        Api.api_service.getMyShareGold().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
//                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    MyShareGoldModel.DatasBean item = result.datas;
                    txtBalance.setText(item.balance);
                    Spanned txtLeft = Spans.with(this).font("淘淘币累计总数量(个)\n", getResources().getDimensionPixelSize(R.dimen.x24), 0xff999999).
                            font(item.total, getResources().getDimensionPixelSize(R.dimen.x32), 0xffFF0030).
                            build();
                    txtInfoLeft.setText(txtLeft);

                    Spanned txtRight = Spans.with(this).font("待领取数量(个)\n", getResources().getDimensionPixelSize(R.dimen.x24), 0xff999999).
                            font(item.freeze, getResources().getDimensionPixelSize(R.dimen.x32), 0xffFF0030).
                            build();
                    txtInfoRight.setText(txtRight);
                });
    }

    public void getExplain(String keyword) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        ShareGoldExplainBean bean = new ShareGoldExplainBean();
        bean.keyword = keyword;
        Api.api_service.getShareGoldExplain(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    Dialog.showPacketComeTips(this, result.datas.title, result.datas.content);
                });
    }

    @OnClick({R.id.btn_back, R.id.txt_right, R.id.btn_work_ing, R.id.btn_work_over, R.id.btn_question})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                break;
            case R.id.btn_work_ing:
                if (select == 0) {
                    return;
                }
                select = 0;
                showTextChange(true);
                pager.setCurrentItem(0);
                break;
            case R.id.btn_work_over:
                if (select == 1) {
                    return;
                }
                select = 1;
                showTextChange(false);
                pager.setCurrentItem(1);
                break;
            case R.id.btn_question:
                getExplain(Const.KeyWord.SHARE_GOLD_ACCOUNT);
                break;
        }
    }
}
